<template>
  <div id="add_user_id">
    <el-dialog
      :title="ruleForm.id === 0 ? '新增用户' : '修改用户'"
      :visible.sync="isShow.flag"
      width="50%"
      :before-close="close"
    >
      <el-form
        ref="ruleForm"
        :rules="rules"
        :model="ruleForm"
        label-width="100px"
        style="text-align: left"
      >
        <el-form-item label="上级菜单" prop="roleid">
          <el-select v-model="ruleForm.roleid" placeholder="请选择账号角色">
            <el-option
              :label="role.rolename"
              :value="role.id"
              v-for="role in role_list"
              :key="role.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="账号名称" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>

        <el-form-item label="账号密码" prop="password">
          <el-input type="password" v-model="ruleForm.password"></el-input>
        </el-form-item>
        <el-form-item label="账号状态" prop="status">
          <el-radio-group v-model="ruleForm.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapActions, mapState } from "vuex";
export default {
  props: ["isShow"],
  data() {
    return {
      ruleForm: {
        id: 0,
        username: "",
        roleid: "",
        password: "",
        status: 1,
      },
      rules: {
        username: {
          required: true,
          message: "用户名不可以为空",
          trigger: "blur",
        },
        roleid: { required: true, message: "菜单不可以为空", trigger: "blur" },
        password: {
          required: true,
          message: "密码不可以为空",
          trigger: "blur",
        },
        status: { required: true, message: "状态不可以为空", trigger: "blur" },
      },
    };
  },
  methods: {
    setRuleForm(obj){     
        this.ruleForm = obj
    },  
    close() {
      this.ruleForm = {
        id: 0,
        username: "",
        roleid: "",
        password: "",
        status: 1,
      };
      //关闭模态框
      this.isShow.flag = false;
    },
    submit() {
      if(this.ruleForm.username == '' && this.ruleForm.password == ''){
        return alert("用户名或密码不得为空")
      }
      var url = "/api/useradd";
       if(this.ruleForm.id != 0){
          url = "/api/useredit"
      } 
      this.$http.post(url, this.ruleForm).then((res) => {
        if (res.code === 200) {
          //清空表单
          this.ruleForm = {
            id: 0,
            username: "",
            roleid: "",
            password: "",
            status: 1,
          };
          //关闭模态框
          this.isShow.flag = false;
          //重置列表
          this.$emit("reloadUser");
        }
      });
    },
    ...mapActions("RoleStore", ["RoleActions"]),
  },
  created() {
    this.RoleActions();
  },
  computed: {
    ...mapState({ role_list: (state) => state.RoleStore.role_list }),
  },
};
</script>

<style>
</style>